<!DOCTYPE HTML>
<html>

<head>

    <meta charset="utf-8">
    <script type="text/javascript" src="jquery.js"></script>

</head>




<body>


    Local:
    <br>
    <video id="localVideo" autoplay></video>
    <br> Remote:
    <br>
    <video id="remoteVideo" autoplay></video>

    error:
    <div id="ee"></div>


    <br>
    <br>
    <br>


    <script>
        var isCaller = window.location.href.split('#')[1];

        var socket = new WebSocket("wss://" + window.location.host);

        var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
        var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
        navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;

        var iceServer = {

            iceServers: [{ url: 'stun:stun01.sipphone.com' },
            { url: 'stun:stun.ekiga.net' },
            { url: 'stun:stun.fwdnet.net' },
            { url: 'stun:stun.ideasip.com' },
            { url: 'stun:stun.iptel.org' },
            { url: 'stun:stun.rixtelecom.se' },
            { url: 'stun:stun.schlund.de' },
            { url: 'stun:stun.l.google.com:19302' },
            { url: 'stun:stun1.l.google.com:19302' },
            { url: 'stun:stun2.l.google.com:19302' },
            { url: 'stun:stun3.l.google.com:19302' },
            { url: 'stun:stun4.l.google.com:19302' },
            { url: 'stun:stunserver.org' },
            { url: 'stun:stun.softjoys.com' },
            { url: 'stun:stun.voiparound.com' },
            { url: 'stun:stun.voipbuster.com' },
            { url: 'stun:stun.voipstunt.com' },
            { url: 'stun:stun.voxgratia.org' },
            { url: 'stun:stun.xten.com' },
            {
                url: 'turn:numb.viagenie.ca',
                credential: 'muazkh',
                username: 'webrtc@live.com'
            },
            {
                url: 'turn:192.158.29.39:3478?transport=udp',
                credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
                username: '28224511:1379330808'
            },
            {
                url: 'turn:192.158.29.39:3478?transport=tcp',
                credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
                username: '28224511:1379330808'
            },

            {
                url: "turn:13.250.13.83:3478?transport=udp",
                username: "YzYNCouZM1mhqhmseWk6",
                credential: "YzYNCouZM1mhqhmseWk6"
            }


            ]
        };


        // var iceServer = {
        //     iceServers: [{
        //         'url': 'stun:stun.l.google.com:19302'
        //     }, {
        //         'url': 'stun:stun2.l.google.com:19302'
        //     }, {
        //         'url': 'stun:stun.iptel.org'
        //     }, {
        //         'url': 'stun:stun.services.mozilla.com'
        //     }, {
        //         'url': 'stun:stun.lovense.com:3478',
        //         'username': '',
        //         'password': ''
        //     }, {
        //         'url': 'turn:stun.lovense.com:3478',
        //         'username': 'webrtc',
        //         'password': 'webrtc',
        //         'credential': 'webrtc'
        //     }]
        // };




        var pc = new PeerConnection(iceServer);

        pc.onicecandidate = function (event) {

            console.log("----", event.candidate)

            if (event.candidate !== null) {
                socket.send(JSON.stringify({
                    "event": "_ice_candidate",
                    "data": {
                        "candidate": event.candidate
                    }
                }));
            }
        };

        pc.onaddstream = function (event) {
            document.getElementById('remoteVideo').src = URL.createObjectURL(event.stream);
        };

        var sendOfferFn = function (desc) {
            pc.setLocalDescription(desc);
            socket.send(JSON.stringify({
                "event": "_offer",
                "data": {
                    "sdp": desc
                }
            }));
        },
            sendAnswerFn = function (desc) {
                pc.setLocalDescription(desc);
                socket.send(JSON.stringify({
                    "event": "_answer",
                    "data": {
                        "sdp": desc
                    }
                }));
            };

        navigator.getUserMedia({
            "audio": false,
            "video": true
        }, function (stream) {
            document.getElementById('localVideo').src = URL.createObjectURL(stream);
            pc.addStream(stream);
            if (isCaller) {

                $('body').append($("<button > ��Է� </button>").click(function () {

                    //pc.remoteDescription();

                    pc.createOffer(sendOfferFn, function (error) {
                        console.log('Failure callback: ' + error);
                    });


                }))


            }
        }, function (error) {
            console.log('getUserMedia error: ' + error);

            document.getElementById('ee').innerHTML = error.name + "   " + error.message;

        });

        socket.onmessage = function (event) {
            console.log(event.data);

            var json = JSON.parse(event.data)

            console.log('onmessage: ', json);

            if (json.event === "_ice_candidate") {
                pc.addIceCandidate(new IceCandidate(json.data.candidate));
            } else {
                pc.setRemoteDescription(new SessionDescription(json.data.sdp));
                if (json.event === "_offer") {
                    pc.createAnswer(sendAnswerFn, function (error) {
                        console.log('Failure callback: ' + error);
                    });
                }
            }
        };
    </script>
</body>

</html>